/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AuthPenSign;

import java.awt.*;
import java.awt.geom.*;
import jpen.*;
import jpen.owner.multiAwt.AwtPenToolkit;
import java.util.ArrayList;
import javax.swing.JTextField;
import AuthPenSign.Signature.Point;

/**
 *
 * @author V-ray
 */
public class PenAdapter extends jpen.event.PenAdapter {

	Graphics2D g2d;
	ArrayList<Point> arrayPoints;
	JTextField[] textFields;
	Point2D.Double prevLoc;
	Point2D.Double curLoc;
	Long timeStart;

	public PenAdapter() {
		this.prevLoc = new Point2D.Double();
		this.curLoc = new Point2D.Double();
		this.timeStart = null;

		AwtPenToolkit.getPenManager().pen.levelEmulator.setPressureTriggerForLeftCursorButton(0.5f);
	}

	public void AddOutput(Graphics2D g2d) {
		this.g2d = g2d;
		this.g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		this.g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
		this.g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	}

	public void AddOutput(ArrayList<Point> arrayPoints) {
		this.arrayPoints = arrayPoints;
	}

	public void AddOutput(JTextField[] textFields) {
		this.textFields = textFields;
	}

	@Override
	public void penLevelEvent(PLevelEvent ev) {

		if (!ev.isMovement()) {
			return;
		}

		if (this.timeStart == null) {
			this.timeStart = System.currentTimeMillis();
		}

		Float pressure = ev.pen.getLevelValue(PLevel.Type.PRESSURE);

		curLoc.x = ev.pen.getLevelValue(PLevel.Type.X);
		curLoc.y = ev.pen.getLevelValue(PLevel.Type.Y);

		if (pressure > 0) {
			if (this.g2d != null) {
				this.g2d.setColor(Color.BLACK);
				this.g2d.setStroke(new BasicStroke(pressure * 2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
				this.g2d.draw(new Line2D.Double(prevLoc, curLoc));
			}
			if (this.arrayPoints != null) {
				this.arrayPoints.add(new Point((double) (System.currentTimeMillis() - this.timeStart), curLoc.x, curLoc.y, pressure));
			}
			if (this.textFields != null) {
				this.textFields[0].setText(((Double) curLoc.x).toString());
				this.textFields[1].setText(((Double) curLoc.y).toString());
				this.textFields[2].setText(pressure.toString());
			}
		}

		prevLoc.setLocation(curLoc);
	}
}
